<template>
  <div class="encounter">
    <div class="search-box">
      <Form inline :label-width="80">
        <FormItem label="ID">
          <Input type="text" v-model="id" placeholder="ID">
          </Input>
        </FormItem>
        <FormItem label="检索条件">
          <Select v-model="paramName" style="width:100px" clearable>
            <Option value="name">姓名</Option>
          </Select>
          <Input type="text" v-model="value" placeholder="" style="width:200px">
          </Input>
        </FormItem>
        <FormItem>
          <Button type="primary" @click="doSearch">搜索</Button>
          <Button type="warning" @click="historySearch">历史版本</Button>
        </FormItem>
      </Form>
    </div>
    <div class="opt">
      <Button type="primary" @click="doSearch">刷新</Button>
      <Button type="success" @click="add">新增</Button>
    </div>
    <div class="data-table">
      <Table border :columns="columns" :data="tableData" size="small" :loading="tableLoading"></Table>
    </div>

    <Drawer
      :title="title"
      v-model="showEdit"
      width="720"
      :mask-closable="false"
      :styles="styles"
    >
      <Form :model="editData" :label-width="80">
        <FormItem label="姓名" prop="patientName">
          <Input v-model="editData.patientName"/>
        </FormItem>
        <FormItem label="就诊类型" prop="visitTypeCode">
          <CommonSelect v-model="editData.visitTypeCode" :code.sync="editData.visitTypeCode"
                        :name.sync="editData.visitTypeName" :data-options="visitTypeDict">
          </CommonSelect>
        </FormItem>
        <FormItem label="入院时间" prop="visitTime">
          <DatePicker type="datetime" v-model="editData.visitTime"/>
        </FormItem>
        <FormItem label="出院时间" prop="dischargeTime">
          <DatePicker type="datetime" v-model="editData.dischargeTime"/>
        </FormItem>
        <FormItem label="主治医生" prop="participant">
          <Input v-model="editData.participant"/>
        </FormItem>
        <FormItem label="就诊原因" prop="visitReason">
          <Input v-model="editData.visitReason"/>
        </FormItem>
        <FormItem label="入院诊断" prop="diagnosis">
          <Input v-model="editData.diagnosis"/>
        </FormItem>
        <FormItem label="入院前位置" prop="origin">
          <Input v-model="editData.origin"/>
        </FormItem>
        <FormItem label="患者来源" prop="adminSourceCode">
          <CommonSelect v-model="editData.adminSourceCode" :code.sync="editData.adminSourceCode"
                        :name.sync="editData.adminSourceName" :data-options="adminSourceDict">
          </CommonSelect>
        </FormItem>
        <FormItem label="患者去向" prop="dischargeDispositionCode">
          <CommonSelect v-model="editData.dischargeDispositionCode" :code.sync="editData.dischargeDispositionCode"
                        :name.sync="editData.dischargeDispositionName" :data-options="dischargeDispositionDict">
          </CommonSelect>
        </FormItem>
        <FormItem label="就诊机构" prop="orgName">
          <Input v-model="editData.orgName"/>
        </FormItem>
      </Form>

      <div class="demo-drawer-footer">
        <Button style="margin-right: 8px" type="primary" @click="onSubmit">确定</Button>
        <Button type="error" @click="showEdit = false">取消</Button>
      </div>
    </Drawer>
  </div>
</template>

<script>
  import CommonSelect from "../../../components/select/commonSelect";

  export default {
    name: "encounter",
    components: {CommonSelect},
    computed: {
      tableData() {
        let jData = [];
        this.$store.state.enc.encounterList.forEach(entry => {
          let re = entry.resource;
          if (re.total && re.total.myStringValue == '0')
            return;
          jData.push({
            url: re.id.myStringValue,
            id: re.id.myUnqualifiedId,
            versionId: re.id.myUnqualifiedVersionId,
            status: re.status ? re.status.myStringValue : '',
            patientName: re.subject ? (re.subject.display ? re.subject.display.myStringValue : '') : '',
            visitTypeCode: re.class_ ? (re.class_.code ? re.class_.code.myStringValue : '') : '',
            visitTypeName: re.class_ ? (re.class_.display ? re.class_.display.myStringValue : '') : '',
            visitTime: re.period ? (re.period.start ? re.period.start.myStringValue : '') : '',
            dischargeTime: re.period ? (re.period.end ? re.period.end.myStringValue : '') : '',
            participant: re.participant ? (re.participant[0].individual ? (re.participant[0].individual.display ? re.participant[0].individual.display.myStringValue : '') : '') : '',
            visitReason: re.reason ? (re.reason[0].text ? re.reason[0].text.myStringValue : '') : '',
            diagnosis: re.diagnosis ? (re.diagnosis[0].condition ? (re.diagnosis[0].condition.display ? re.diagnosis[0].condition.display.myStringValue : '') : '') : '',
            origin: re.hospitalization ? (re.hospitalization.origin ? re.hospitalization.origin.display.myStringValue : '') : '',
            adminSourceCode: re.hospitalization ? (re.hospitalization.admitSource ? (re.hospitalization.admitSource.coding ? re.hospitalization.admitSource.coding[0].code.myStringValue : '') : '') : '',
            adminSourceName: re.hospitalization ? (re.hospitalization.admitSource ? (re.hospitalization.admitSource.coding ? re.hospitalization.admitSource.coding[0].display.myStringValue : '') : '') : '',
            dischargeDispositionCode: re.hospitalization ? (re.hospitalization.dischargeDisposition ? (re.hospitalization.dischargeDisposition.coding ? re.hospitalization.dischargeDisposition.coding[0].code.myStringValue : '') : '') : '',
            dischargeDispositionName: re.hospitalization ? (re.hospitalization.dischargeDisposition ? (re.hospitalization.dischargeDisposition.coding ? re.hospitalization.dischargeDisposition.coding[0].display.myStringValue : '') : '') : '',
            orgName: re.serviceProvider ? (re.serviceProvider.display ? re.serviceProvider.display.myStringValue : '') : ''
          })
        })
        return jData;
      }
    },
    data() {
      return {
        type: 'Encounter',
        id: null,
        paramName: null,
        value: null,
        tableLoading: false,
        showEdit: false,
        isUpdate: false,
        title: '',
        editData: {},
        visitTypeDict: [{code: 'IMP', name: '住院'}],
        adminSourceDict: [{code: 'hosp-trans', name: '转诊'}],
        dischargeDispositionDict: [{code: 'home', name: '回家'}],
        styles: {
          height: 'calc(100% - 55px)',
          overflow: 'auto',
          paddingBottom: '53px',
          position: 'static'
        },
        columns: [
          {
            title: 'ID',
            key: 'id'
          },
          {
            title: '版本',
            key: 'versionId'
          },
          {
            title: '就诊状态',
            key: 'status',
          },
          {
            title: '姓名',
            key: 'patientName'
          },
/*          {
            title: '就诊类型编码',
            key: 'visitTypeCode'
          },*/
          {
            title: '就诊类型',
            key: 'visitTypeName'
          },
          {
            title: '就诊（入院）时间',
            key: 'visitTime'
          },
          {
            title: '出院时间',
            key: 'dischargeTime'
          },
          {
            title: '主治医生',
            key: 'participant',
          },
          {
            title: '就诊原因',
            key: 'visitReason'
          },
          {
            title: '入院诊断',
            key: 'diagnosis'
          },
          {
            title: '入院前位置',
            key: 'origin'
          },
/*          {
            title: '患者来源编码',
            key: 'adminSourceCode'
          },*/
          {
            title: '患者来源',
            key: 'adminSourceName'
          },
/*          {
            title: '患者去向编码',
            key: 'dischargeDispositionCode'
          },*/
          {
            title: '患者去向',
            key: 'dischargeDispositionName'
          },
          {
            title: '就诊机构',
            key: 'orgName'
          },

          {
            title: "操作",
            width: 120,
            type: "render",
            render: (h, params) => {
              return h('div', {
                class: 'btn-wrap'
              }, [
                h('a', {
                  on: {
                    click: () => {
                      this.update(params.row)
                    }
                  }
                }, '编辑'),
                h('Divider', {
                  props: {type: 'vertical'},
                }),
                h('a', {
                  on: {
                    click: () => {
                      this.remove(params.row)
                    }
                  }
                }, '删除'),
              ])
            }
          }
        ]
      }
    },
    created() {
      this.doSearch();
    },
    methods: {
      doSearch() {
        this.tableLoading = true;
        this.$store.dispatch('getEncounterList', {
          type: this.type,
          id: this.id,
          paramName: this.paramName,
          value: this.value,
        }).then(() => {
          this.tableLoading = false;
        }).catch(res => {
          this.$Notice.error({
            title: res.error,
            desc: res.message
          });
          this.tableLoading = false;
        })
      },
      historySearch() {
        if (!this.id) {
          this.$Message.error('请输入ID');
          return;
        }
        this.tableLoading = true;
        this.$store.dispatch('getHistoryEncounterList', {
          type: this.type,
          id: this.id,
        }).then(() => {
          this.tableLoading = false;
        })
      },
      add() {
        this.editData = {
          patientName: '张三',
          visitTypeCode: 'IMP',
          visitTypeName: '住院',
          visitTime: '2018-10-28 12:46:41',
          dischargeTime: '2018-11-01 12:46:48',
          participant: '李四',
          visitReason: '髋关节置换术后康复治疗',
          diagnosis: '髋关节置换术后',
          origin: '北大国际医院',
          adminSourceCode: 'hosp-trans',
          adminSourceName: '转诊',
          dischargeDispositionCode: 'home',
          dischargeDispositionName: '回家',
          orgName: '泰康燕园康复医院'
        };
        this.showEdit = true;
        this.isUpdate = false;
        this.title = '新增就诊';
      },
      update(row) {
        this.editData = JSON.parse(JSON.stringify(row));
        this.showEdit = true;
        this.isUpdate = true;
        this.title = '更新就诊';
      },
      remove(row) {
        this.$store.dispatch('deleteEncounter', {
          type: this.type,
          id: row.id
        }).then(() => {
          this.$Message.success('删除成功');
          this.id = row.id;
          this.doSearch();
        });
      },
      onSubmit() {
        if (this.isUpdate) {
          this.$store.dispatch('updateEncounter', Object.assign({type: this.type}, this.editData)).then(() => {
            this.$Message.success('更新成功');
            this.showEdit = false;
            this.id = this.editData.id;
            this.doSearch();
          });
        } else {
          this.$store.dispatch('createEncounter', Object.assign({type: this.type}, this.editData)).then((res) => {
            this.$Message.success('新增成功');
            this.showEdit = false;
            this.id = res.data.myId.myUnqualifiedId;
            this.doSearch();
          });
        }
      }
    }
  }
</script>

<style scoped>
  .data-table {
    margin-top: 10px;
  }

  .demo-drawer-footer {
    width: 100%;
    position: absolute;
    bottom: 0;
    left: 0;
    border-top: 1px solid #e8e8e8;
    padding: 10px 16px;
    text-align: right;
    background: #fff;
  }
</style>
